<template>
    <el-table :data="filterTableData" style="width: 100%">
      <el-table-column label="Date" prop="date" />
      <el-table-column label="Name" prop="name" />
      <el-table-column label="状态" prop="status" />
      <el-table-column align="right">
        <template #header>
          <el-input v-model="search" size="small" placeholder="Type to search" />
        </template>
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >Edit</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >
                Delete
            </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" title="Shipping address">
    <el-form :model="form">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="出生地" :label-width="formLabelWidth">
        <el-select v-model="form.region" placeholder="Please select a zone">
          <el-option label="Zone No.1" value="shanghai" />
          <el-option label="Zone No.2" value="beijing" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="editFun">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>

  </template>
  
  <script lang="ts" setup>
  import { computed, ref, reactive } from 'vue'

const dialogFormVisible = ref(false)
const formLabelWidth = '140px'

const form = reactive({
  name: '',
  region: '',
  date1: '',
  date2: '',
  delivery: false,
  type: [],
  resource: '',
  desc: '',
})
  interface User {
    date: string
    name: string
    address: string,
    status: string
  }
  
  const search = ref('')
  const filterTableData = computed(() =>
    tableData.filter(
      (data) =>
        !search.value ||
        data.name.toLowerCase().includes(search.value.toLowerCase())
    )
  )
  const handleEdit = (index: number, row: User) => {
    console.log(index, row)
    currIndex.value = index;
    const {name} = row;
    form.name = name;

    dialogFormVisible.value=true;

  }
  const handleDelete = (index: number, row: User) => {
    console.log(index, row)
  }

  const currIndex = ref<number>(0);

  const data: User[] = [
    {
      date: '2016-05-03',
      name: 'Tom',
      status:'可用',
      address: 'No. 189, Grove St, Los Angeles',
    },
    {
      date: '2016-05-02',
      name: 'John',
      status:'不可用',
      address: 'No. 189, Grove St, Los Angeles',
    },
    {
      date: '2016-05-04',
      name: 'Morgan',
      status:'可用',
      address: 'No. 189, Grove St, Los Angeles',
    },
    {
      date: '2016-05-01',
      name: 'Jessy',
      status:'不可不用',
      address: 'No. 189, Grove St, Los Angeles',
    },
  ]

  const tableData = reactive(data)

  // 保存修改的内容
  const editFun = ()=>{
    dialogFormVisible.value = false;
    const {name } = form;
    console.log('name',name);
    // TODO 1)ajax 这里做后端接口的网络请求 axios.post('/test/saveData',{name})
    // TODO 2) 方式一上一个修改的接口返回正确的状态 然后ajax 接口请求 tableData的数据 

    tableData[currIndex.value].name = name;
  }
  
  
  </script>
  